/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { initByFAJsEmptyApp } from '../../common/common';
import { HOS_SIGNATURE_TEMPLATE, Tasks } from '../constants';
import { buildTest, checkSuccessful, configJsProject } from '../utils';

describe('Hos Api8 js wearable test', () => {
  test.concurrent('Test build hap package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot, undefined,
        [{ moduleName: 'entry', deviceTypes: ['wearable'] }],
        {
          sdkVersion: 8,
          signConfig: HOS_SIGNATURE_TEMPLATE,
        });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { rich: true });
    }, { sign: true });
  });

  test.concurrent('Test build hap package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot,
        undefined,
        [{ moduleName: 'entry', deviceTypes: ['wearable'] }], { sdkVersion: 8 });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { rich: true, signed: false });
    }, { sign: false });
  });

  test.concurrent('Test build app package with signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };

      configJsProject(projectRoot, undefined, [{ moduleName: 'entry', deviceTypes: ['phone', 'wearable'] }], {
        sdkVersion: 8,
        signConfig: HOS_SIGNATURE_TEMPLATE,
      });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP, { rich: true });
    }, { sign: true });
  });

  test.concurrent('Test build app package without signature', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'project',
        product: 'default',
        tasks: Tasks.ASSEMBLE_APP,
      };

      configJsProject(projectRoot, undefined, [{
        moduleName: 'entry',
        deviceTypes: ['phone', 'wearable'],
      }], { sdkVersion: 8 });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_APP, { rich: true, signed: false });
    }, { sign: false });
  });

  // api8 瘦设备工程修改为api9
  test.concurrent('Test build api9 wearable project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot, [{
        moduleName: 'entry',
        updateTarget: [{
          name: 'default',
          runtimeOS: 'HarmonyOS',
        }],
      }],
      [{
        moduleName: 'entry',
        deviceTypes: ['wearable'],
      }],
      {
        sdkVersion: 9
      });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { rich: true });
    });
  });

  // 能构建API8 wearable应用的测试包
  test.concurrent('Test build test module of api8 wearable project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        module: 'entry@ohosTest',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot, undefined, [{ moduleName: 'entry', deviceTypes: ['wearable'] }], {
        sdkVersion: 8,
        signConfig: HOS_SIGNATURE_TEMPLATE,
      });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'ohosTest', Tasks.ASSEMBLE_HAP, { rich: true });
    });
  });

  // 改造工程为ohos工程
  test.concurrent('Test build ohos project', async () => {
    await initByFAJsEmptyApp(async (projectRoot) => {
      const commands = {
        mode: 'module',
        product: 'default',
        module: 'entry@default',
        tasks: Tasks.ASSEMBLE_HAP,
      };

      configJsProject(projectRoot, [{
        moduleName: 'entry',
        updateTarget: [
          {
            name: 'default',
            runtimeOS: 'OpenHarmony',
          },
        ],
      }],
      [{
        moduleName: 'entry',
        deviceTypes: ['wearable'],
      }],
      {
        sdkVersion: 8,
        updateProducts: [
          { name: 'default', bundleName: 'com.example.myapplication' },
        ],
      });

      checkSuccessful(projectRoot, commands);
      buildTest(projectRoot, 'entry', 'default', Tasks.ASSEMBLE_HAP, { rich: true });
    });
  });
});
